﻿using System.Windows;
using System.Windows.Controls;
using GalaSoft.MvvmLight.Messaging;
using Drafter.Messages;

namespace Drafter
{
    /// <summary>
    /// Description for EditView.
    /// </summary>
    public partial class EditView : UserControl
    {
        /// <summary>
        /// Initializes a new instance of the EditView class.
        /// </summary>
        public EditView()
        {
            InitializeComponent();

            this.Loaded += new RoutedEventHandler(EditView_Loaded);

            Messenger.Default.Register<OpenFileDialogMessage>(this, ShowOpenFileDialog);
            Messenger.Default.Register<SaveFileDialogMessage>(this, ShowSaveFileDialog);
            Messenger.Default.Register<PrintDialogMessage>(this, PrintFileDialog);
        }

        void EditView_Loaded(object sender, RoutedEventArgs e)
        {
            this.txtText.Focus();
        }
        public void ShowOpenFileDialog(OpenFileDialogMessage dialogMessage)
        {
            string filename = null;
            Microsoft.Win32.OpenFileDialog dialog = new Microsoft.Win32.OpenFileDialog();
            dialog.DefaultExt = dialogMessage.DefaultExtension;
            dialog.Filter = dialogMessage.Filter;
            bool? result = dialog.ShowDialog();

            if (result == true)
            {
                filename = dialog.FileName;
            }

            dialogMessage.ProcessCallback(filename);
        }


        public void ShowSaveFileDialog(SaveFileDialogMessage dialogMessage)
        {
            string filename = null;
            Microsoft.Win32.SaveFileDialog dialog = new Microsoft.Win32.SaveFileDialog();
            dialog.DefaultExt = dialogMessage.DefaultExtension;
            dialog.Filter = dialogMessage.Filter;
            if (dialog.ShowDialog() == true)
            {
                 filename = dialog.FileName;
            }
            dialogMessage.ProcessCallback(filename);
        }

        public void PrintFileDialog(PrintDialogMessage dialogMessage)
        {
             PrintDialog printDialog = new PrintDialog();
             if ((bool)printDialog.ShowDialog().GetValueOrDefault())
             {
                 printDialog.PrintDocument(dialogMessage.Paginator, dialogMessage.Title);
                
             }
        }

        private void MenuIcon_MouseUp(object sender, System.Windows.Input.MouseButtonEventArgs e)
        {
        	menuPopup.IsOpen=true;
        }
		
		public void ShowExportFileDialog(SaveFileDialogMessage dialogMessage)
        {
            string filename = null;
            Microsoft.Win32.SaveFileDialog dialog = new Microsoft.Win32.SaveFileDialog();
            dialog.DefaultExt = ".draft";
            dialog.Filter = "Drafts|*.draft";
            if (dialog.ShowDialog() == true)
            {
                 filename = dialog.FileName;
            }
            dialogMessage.ProcessCallback(filename);
        }

		private void Button_Click(object sender, System.Windows.RoutedEventArgs e)
		{
			int a=10;
			// TODO: Add event handler implementation here.
		}
    }
}